#include <iostream>
#include <string>
#include <memory>
#include <sstream>
#include <jsoncpp/json/json.h>

std::string serialize()
{
    Json::Value root;
    root["name"] = "张三";
    root["age"] = 18;
    root["sex"] = "男";

    Json::StreamWriterBuilder builder;
    std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
    std::stringstream ss; 
    writer->write(root, &ss);

    std::cout << ss.str() << std::endl;
    return ss.str();
}

void deserialize(std::string json_string)
{
    Json::Value root;
    Json::CharReaderBuilder builder;
    std::unique_ptr<Json::CharReader> reader(builder.newCharReader());

    std::string ss;
    reader->parse(json_string.c_str(), json_string.c_str()+json_string.size(), &root, &ss);

    std::string name = root["name"].asString();
    int age = root["age"].asInt();
    std::string sex = root["sex"].asString();

    std::cout << name << " " << age << " " << sex << std::endl;
}

int main()
{
    std::string s = serialize();
    deserialize(s);
    return 0;
}